package com.qidianit.mes.md.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.md.mapper.UnitMapper;
import com.qidianit.mes.md.service.UnitService;
import com.qidianit.mes.pojo.Unit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 奇幻夜之梦
 * @Company 起点编程
 * @Description
 * @Time 2024/10/17 17:15
 */
@Service
public class UnitServiceImpl implements UnitService {
    @Autowired
    private UnitMapper unitsMapper;
    @Override
    public MESResult selectByPage(Unit unit, Integer page, Integer limit) {
        //开启分页  参数1： 页数  参数2： 每页大小
        PageHelper.startPage(page,limit);
        //查询全部
        List<Unit> units = unitsMapper.selectByPage(unit);
        //创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<Unit> pageInfo = new PageInfo<>(units);
        // 将pageInfo对象设置到QdResult中
        return MESResult.success(pageInfo);// 返回结果
    }

    @Override
    public MESResult save(Unit unit) {
        if (unit.getUnitCode() == null || unit.getUnitCode() == "") {
            return MESResult.error("单位编码不能为空");
        }
        if (unit.getUnitName() == null || unit.getUnitName() == "") {
            return MESResult.error("单位名称不能为空");
        }
        if (unit.getPrimaryFlag() == null) {
            return MESResult.error("是否是主单位必须选其一");
        }
        if (unit.getPrimaryFlag() == 2) {
            if (unit.getPrimaryId() == null || unit.getConversionScale() == null)
            return MESResult.error("主单位和换算比例必须选择");
        }
        if (unit.getUnitStatus() == null) {
            return MESResult.error("是否启用必须选其一");
        }
        unit.setCreateTime(new Date());
        unit.setModifyTime(new Date());
        return unitsMapper.save(unit) == 1 ? MESResult.success(unit):MESResult.error();
    }

    @Override
    public MESResult updateUnit(Unit unit) {
        if (unit.getUnitCode() == null || unit.getUnitCode() == "") {
            return MESResult.error("单位编码不能为空");
        }
        if (unit.getUnitName() == null || unit.getUnitName() == "") {
            return MESResult.error("单位名称不能为空");
        }
        if (unit.getPrimaryFlag() == 2) {
            if (unit.getPrimaryId() == null || unit.getConversionScale() == null)
                return MESResult.error("主单位和换算比例不能为空");
        }
        if (unit.getPrimaryFlag() == 1) {
            unit.setPrimaryId(null);
            unit.setConversionScale(null);
        }
        return unitsMapper.updateById(unit) == 1?MESResult.success():MESResult.error();
    }

    @Override
    public MESResult selectById(Long unitId) {
        return MESResult.success(unitsMapper.selectById(unitId));
    }

    @Override
    public MESResult delete(Long unitId) {
        // 修改状态
        return MESResult.isSuccess(unitsMapper.deleteById(unitId));
    }

    @Override
    public MESResult deletes(String[] ids) {
        return MESResult.success(Constant.RESPONSE_STATUS_OK,"OK",unitsMapper.updateUnitsByIds(ids,Constant.DATA_STATUS_DELETE));
    }

    @Override
    public MESResult listUnits() {
        return MESResult.success(unitsMapper.listUnits());
    }
}

